﻿/*
DotNetMQ - A Complete Message Broker For .NET
Copyright (C) 2011 Halil ibrahim KALKAN

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using ELab.Serialization.Data;
using System;
using System.Drawing;
using System.Windows.Forms;
using ELab.ImageProcess;
using System.Drawing.Imaging;
namespace ELab.Teacher
{
    public partial class NewUserForm : Form
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FullName { get; set; }
        public string IPAddress { get; set; }
        public UserType UserType { get; set; }
        public Byte[] Avatar { get; set; }
        public Survey[] Surveys { get; set; }

        public NewUserForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var userName = txtUserName.Text.Trim();
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("User name can not be empty!");
                return;
            }

            var password = txtPassword.Text.Trim();
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Password can not be empty!");
                return;
            }

            var fullname = txtFullName.Text.Trim();
            if (string.IsNullOrEmpty(fullname))
            {
                MessageBox.Show("Full name can not be empty!");
                return;
            }

            var ipAddress = txtIPAddress.Text.Trim();
            if (string.IsNullOrEmpty(ipAddress))
            {
                MessageBox.Show("IP Address can not be empty!");
                return;
            }
            IPAddress = ipAddress;
            FullName = fullname;
            UserName = userName;
            Password = password;
            if (rdoStudent.Checked) this.UserType = UserType.Student;
            else this.UserType = UserType.Teacher;
            Close();
        }

        private void AddNewApplicationForm_Load(object sender, EventArgs e)
        {
            PrepareForm();
        }

        /// <summary>
        /// Prepares form.
        /// </summary>
        private void PrepareForm()
        {
            Left = (Screen.GetWorkingArea(this).Width - Width) / 2;
            Top = (Screen.GetWorkingArea(this).Height - Height) / 2;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
            dialog.Title = "Please select an image file.";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var img = Image.FromFile(dialog.FileName);
                int height = 200;
                int width = img.Width * 200 / img.Height; 
                pibAvatar.Image = img.Resize(width, height);
                this.Avatar = pibAvatar.Image.ToByteArray(ImageFormat.Jpeg);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            pibAvatar.Image = null;
            this.Avatar = null;
        }
    }
}
